

class Main extends eui.UILayer {

    private static _ins: Main;
    public static get instance(): Main {
        return this._ins || (this._ins = new Main());
    }
    constructor() {
        super();
        Main._ins = this;
    }

    protected createChildren(): void {
        super.createChildren();

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

        egret.registerImplementation("eui.IAssetAdapter", new AssetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter);

        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private async runGame() {
        await this.loadResource()
        this.addChild(SceneManager.instance);
        // await platform.login();
        // const userInfo = await platform.getUserInfo();
        // console.log(userInfo);
    }

    private async loadResource() {
        try {
            const loadingView = new LoadingView;
            this.stage.addChild(loadingView);
            await RES.loadConfig("resource/default.res.json", "resource/");
            // await this.loadTheme();
            await RES.loadGroup("preload", 0, loadingView);
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }

    // private loadTheme() {
    //     return new Promise((resolve, reject) => {
    //         let theme = new eui.Theme("resource/default.thm.json", this.stage);
    //         theme.addEventListener(eui.UIEvent.COMPLETE, () => {
    //             resolve();
    //         }, this);
    //     })
    // }

    private createBitmapByName(name: string): egret.Bitmap {
        let result = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }

}
